package com.itheima.tlias08_14.controller;

import com.itheima.tlias08_14.annotation.Log;
import com.itheima.tlias08_14.dto.SelectByIdClazzDTO;
import com.itheima.tlias08_14.dto.SelectDataTimeClazzDTO;
import com.itheima.tlias08_14.entity.Clazz;
import com.itheima.tlias08_14.paramobject.InsertClazz;
import com.itheima.tlias08_14.paramobject.SelectClazz;
import com.itheima.tlias08_14.service.impl.ClazzServiceImp;
import com.itheima.tlias08_14.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
public class ClazzController {

    @Resource
    private ClazzServiceImp csImp;
    @GetMapping("/clazzs/list")
    public ResultUtil  selectAllClazz() {
        List<SelectDataTimeClazzDTO> selectByIdClazzDTOS = csImp.selectAllClazz();
        return  ResultUtil.success(selectByIdClazzDTOS) ;
    }
    @GetMapping("/clazzs")
    public ResultUtil selectClazz(SelectClazz selectClazz) {
        return csImp.selectClazz(selectClazz);
    }

    @DeleteMapping("/clazzs/{id}")
    @Log
    public ResultUtil deleteById(@PathVariable("id") Integer id) {
        return csImp.deleteClazzById(id);
    }

    @PostMapping("/clazzs")
    @Log
    public ResultUtil insertClazz(@RequestBody InsertClazz insertClazz) {
        int i = csImp.insertClazz(insertClazz);
        return i == 1 ? ResultUtil.success() : ResultUtil.failure();
    }

    @GetMapping("/clazzs/{id}")
    public ResultUtil selectById(@PathVariable("id") Integer id) {
        SelectByIdClazzDTO selectByIdClazzDTO = csImp.selectClazzById(id);
        return selectByIdClazzDTO != null ? ResultUtil.success(selectByIdClazzDTO) : ResultUtil.failure();
    }

    @PutMapping("/clazzs")
    @Log
    public ResultUtil updateClazz(@RequestBody Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now().toString());
        int i = csImp.updateClazz(clazz);
        return i == 1 ? ResultUtil.success() : ResultUtil.failure();
    }



}
